package com.touzhijia.authority.controller;

import com.github.pagehelper.PageInfo;
import com.touzhijia.authority.domain.dto.request.PermissionAddReq;
import com.touzhijia.authority.domain.dto.request.PermissionQueryByRoleReq;
import com.touzhijia.authority.domain.dto.request.PermissionQueryReq;
import com.touzhijia.authority.domain.dto.request.PermissionUpdateReq;
import com.touzhijia.authority.domain.dto.response.PermissionByRoleRes;
import com.touzhijia.authority.domain.entity.Permission;
import com.touzhijia.authority.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Api(description = "权限管理")
@RestController
@RequestMapping("/api")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ApiOperation("获取权限信息")
    @GetMapping("/permission/{permission}")
    public Permission getPermission(@PathVariable("permission") String permission) {
        return permissionService.getPermission(permission);
    }

    @ApiOperation("查询权限")
    @GetMapping("/permission")
    public PageInfo<Permission> findPermission(@Validated PermissionQueryReq queryReq) {
        return permissionService.findPermission(queryReq);
    }

    @ApiOperation("新增权限信息")
    @PostMapping("/permission")
    public Permission addPermission(@Validated @RequestBody PermissionAddReq addReq) {
        return permissionService.addPermission(addReq);
    }

    @ApiOperation("删除权限信息")
    @DeleteMapping("/permission/{permission}")
    public Permission delPermission(@PathVariable("permission") String permission) {
        return permissionService.del(permission);
    }

    @ApiOperation("修改权限信息")
    @PutMapping("/permission/{permission}")
    public Permission updatePermission(@PathVariable("permission") String permission, @Validated @RequestBody PermissionUpdateReq updateReq) {
        return permissionService.updatePermission(permission, updateReq);
    }

    @ApiOperation("根据角色查询权限")
    @GetMapping("/permission/role")
    public PageInfo<PermissionByRoleRes> findPermissionByRole(@Validated PermissionQueryByRoleReq queryReq) {
        return permissionService.findPermissionByRole(queryReq);
    }
}
